/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bungabungaball.gamestates;

import bungabungaball.GlobalSettings;
import static bungabungaball.gamestates.MainMenu.font;
import bungabungaball.gamestates.menu.Menu;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import bungabungaball.gamestates.menu.MenuEntry;
import org.newdawn.slick.Color;

/**
 * A game State presented when a game is over
 *
 * @author Ingo Klose, Stephan Meyer, Sebastian Lehmann
 * @version 1.0
 */
public class GameEnd extends BasicGameState implements Menu {

    /**
     * The id of the game state
     */
    private int stateID = -1;
    /**
     * The mouse position x
     */
    private int mouseX;
    /**
     * The mouse position y
     */
    private int mouseY;
    /**
     * A menu entry for going to main menu
     */
    private MenuEntry menu;
    /**
     * A menu entry for having a rematch
     */
    private MenuEntry rematch;
    /**
     * A menu entry for showing the replay
     */
    private MenuEntry replay;

    /**
     * Constructor
     *
     * @param stateID
     */
    public GameEnd(int stateID) {
        this.stateID = stateID;
    }

    /**
     * Get the ID of the state
     *
     * @return the ID of the state
     */
    @Override
    public int getID() {
        return stateID;
    }

    /**
     * Create all Menu entries
     *
     * @param gc
     * @param sbg
     * @throws SlickException
     */
    @Override
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
        menu = new MenuEntry();
        menu.text = "Menü";
        menu.x = 100;
        menu.y = 150;
        menu.w = 120;
        menu.h = 15;

        rematch = new MenuEntry();
        rematch.text = "Rematch";
        rematch.x = 100;
        rematch.y = 190;
        rematch.w = 120;
        rematch.h = 15;

        replay = new MenuEntry();
        replay.text = "Replay";
        replay.x = 100;
        replay.y = 230;
        replay.w = 120;
        replay.h = 15;
    }

    /**
     * *
     * Render the Menu
     *
     * @param gc
     * @param sbg
     * @param g
     * @throws SlickException
     */
    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        g.setColor(Color.red);
        g.setFont(font);
        g.drawString("Spielende", 100, 100);

        if (GlobalSettings.winner == 1) {
            g.setColor(Color.magenta);
            g.drawString("Roter Spieler gewinnt", 100, 50);
        } else {
            if (GlobalSettings.winner == 2) {
                g.setColor(Color.magenta);
                g.drawString("Blauer Spieler gewinnt", 100, 50);
            }
        }


        g.setColor(menu.checkMouseIn(mouseX, mouseY) ? Color.green : Color.white);
        g.drawString(menu.text, menu.x, menu.y);

        g.setColor(rematch.checkMouseIn(mouseX, mouseY) ? Color.green : Color.white);
        g.drawString(rematch.text, rematch.x, rematch.y);

        g.setColor(replay.checkMouseIn(mouseX, mouseY) ? Color.green : Color.white);
        g.drawString(replay.text, replay.x, replay.y);

    }

    /**
     * *
     * Handle all Input
     *
     * @param gc
     * @param sbg
     * @param delta
     * @throws SlickException
     */
    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {

        Input input = gc.getInput();
        if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
            if (menu.checkMouseIn(mouseX, mouseY)) {
                menu.clickPause();
                sbg.enterState(bungabungaball.BungaBungaBall.MAINMENUSTATE);
            }

            if (rematch.checkMouseIn(mouseX, mouseY)) {
                rematch.clickPause();
                sbg.enterState(bungabungaball.BungaBungaBall.GAMEPLAYSTATESingle);
            }

            if (replay.checkMouseIn(mouseX, mouseY)) {
                replay.clickPause();
                GlobalSettings.currentGameMode = GlobalSettings.gameModes.Replay;
                sbg.enterState(bungabungaball.BungaBungaBall.GAMEPLAYSTATESingle);
            }
        }

        mouseX = input.getAbsoluteMouseX();
        mouseY = input.getAbsoluteMouseY();
    }
}
